package com.xncoding.trans.service.impl;

import com.xncoding.trans.entity.Menu;
import com.xncoding.trans.pojo.MenuNode;
import com.xncoding.trans.service.MenuService;
import com.xncoding.trans.service.RedisCacheUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
	Log log = LogFactory.getLog(this.getClass());

	@Autowired
	public RedisTemplate redisTemplate;
	@Resource
	RedisCacheUtils r;

	@CacheEvict (cacheNames ={ "menu","menuTree"},allEntries=true)
	public void addMenu(Menu menu) {
		
	}

	@Cacheable(cacheNames="menu")
	public Menu getMenu(String id) {
		log.info("call service getMenu " + id);
		// 模拟
		Menu menu = new Menu();
		menu.setCode("test");
		menu.setId(id);
		menu.setName("菜单" + id);
		menu.setParentId(1l);

		return menu;
	}

	@Cacheable("menuTree")
	public MenuNode getMenuTree() {

		log.info("call menu tree ");
		Menu root = new Menu();
		root.setCode("root");
		root.setId("1");
		root.setName("系统管理");
		root.setParentId(null);

		Menu menu = new Menu();
		menu.setCode("menu");
		menu.setId("2");
		menu.setName("菜单管理");
		menu.setParentId(1l);

		MenuNode tree = new MenuNode();
		tree.setMenu(root);
		tree.setParent(null);

		MenuNode menuTree = new MenuNode();
		menuTree.setMenu(menu);
		menuTree.setParent(tree);
		tree.getChildren().add(menuTree);

		return tree;

	}

//	@Cacheable(value = "treeObjTest",unless = "#result == null or #m.code eq 'menu'")
//	@Cacheable(value = "treeObjTestAll",key = "#m.id", keyGenerator = "myKeyGenerator",unless = "#result == null or #m.code eq 'menu' ")
	@Cacheable(value = "treeObjTestAll", keyGenerator = "myKeyGenerator")
	public List<Menu> treeObjTest(Menu m) {

		log.info("call menu tree ");
		Menu root = new Menu();
		root.setCode("root");
		root.setName("系统管理");
		root.setParentId(null);

		Menu menu = new Menu();
		menu.setCode("menu");
		menu.setName("菜单管理");

		List<Menu> list = new ArrayList<>();
		list.add(root);
		list.add(menu);

		return list;

	}

//	@CacheableADD(value = "trl01",unless = "#result == null or #m.code eq 'menu' ")
	@Cacheable(value = "trl01",condition = "#m.code eq '0'",unless = "#result == null or #m.code eq 'menu' ")
	public List<Menu> treeObjTest01(Menu m) {

		log.info("call menu tree ");
		Menu root = new Menu();
		root.setCode("root");
		root.setName("系统管理");
		root.setParentId(null);

		Menu menu = new Menu();
		menu.setCode("menu");
		menu.setName("菜单管理");

		List<Menu> list = new ArrayList<>();
		list.add(root);
		list.add(menu);

		return list;

	}

	public void setRCahe(){
		//		trl01::Menu{id=null, code='', name='null', parentId=null}
//		r.setCacheObject("trl01::"+"PU001","1212121231212321");
		redisTemplate.opsForValue().set("trl01::"+"PU001","1212121231212321");;
	}


	@Cacheable(value = "trl02",unless = "#result == null or #m.code eq 'menu' ")
	public List<Menu> treeObjTest02(Menu m) {

		log.info("call menu tree ");
		Menu root = new Menu();
		root.setCode("root");
		root.setName("系统管理");
		root.setParentId(null);

		Menu menu = new Menu();
		menu.setCode("menu");
		menu.setName("菜单管理");

		List<Menu> list = new ArrayList<>();
		list.add(root);
		list.add(menu);

		return list;

	}
	


}
